package com.mi.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mi.bean.GoodsBean;
import com.mi.service.impl.AddGoodsService;
import com.mi.utils.MyUtils;

/**
 * Servlet implementation class AddGoodsAction
 */
@WebServlet("/AddGoodsAction")
public class AddGoodsAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 添加购物车请求处理页面
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int goods_id = (int)request.getSession().getAttribute("goods_id");
/*		
		String loginId = (String) request.getSession().getAttribute("sid");
 		String userId = request.getSession().getAttribute("sid").toString();*/
		
		String customer_id = (String)request.getSession().getAttribute("sid");
		System.out.println("AddGoodsAction-前台页面发送的商品id:"+goods_id);
		System.out.println("AddGoodsAction-用户id:"+customer_id);
		
		// 查询商品信息
		List<GoodsBean> lstGoodsInfo =  MyUtils.getInstance(AddGoodsService.class).queryGoodsInfo(goods_id);
		GoodsBean goods = lstGoodsInfo.get(0); 
		// 根据商品信息和用户id确定往购物车内添加的商品信息(数量默认为1)
		int i = MyUtils.getInstance(AddGoodsService.class).addGod2Shopcart(goods, customer_id);
		
		System.out.println("加入购物车结果："+i);
		
		if(i > 0) {
			// 请求成功
			// 对ajax请求进行响应
			PrintWriter pw = response.getWriter();
			// write方法写入字符串
			pw.write("商品添加成功！");
			pw.flush();
		}
		
	}

}
